<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .father {
      margin: 100px auto;
      width: 200px;
      height: 200px;
      background-color: purple;

      display: flex;
      justify-content: center;
      align-items: center;
    }

    .son {
      width: 100px;
      height: 100px;
      background-color: pink;
      color: #eee;

      display: flex;
      justify-content: center;
      align-items: center;
    }
  </style>
</head>
<body>
  <div class="father">
    <div class="son">son盒子</div>
  </div>
</body>
<script>
  // dom事件流的三个阶段
  // 1.JS代码红只能执行捕获或者冒泡其中的一个阶段
  // 2.onclick 和 attachEvent （ie9一下版本适用）只能得到冒泡阶段
  // 3.捕获阶段：如果attachEventListener第三个参数是true，则处于捕获阶段  document→html→body→father→son
  // 先执行father后执行son
  var son = document.querySelector('.son');
  son.attachEventListener('click', function() {
    alert('son');
  }, true);

  var father = document.querySelector('.father');
  father.attachEventListener('click', function() {
    alert('father');
  }, true);
  // 4.冒泡阶段：如果attachEventListener第三个参数是false或者省略，则处于冒泡阶段  son→father→body→html→document
  // 先执行son再执行father最后执行document
  var son = document.querySelector('.son');
  son.attachEventListener('click', function() {
    alert('son');
  }, false);

  var father = document.querySelector('.father');
  father.attachEventListener('click', function() {
    alert('father');
  }, false);

  document.attachEventListener('click', function() {
    alert('document');
  })
</script>
</html>